package com.colorlight.cloud.complete.entity.enumerate;

import com.colorlight.cloud.common.exception.ParameterException;
import com.colorlight.cloud.common.model.ExceptionEnum;
import com.colorlight.cloud.gateway.api.message.constraint.basics.ienum.EnumTrans;
import com.colorlight.iot.proto.complete.entity.CompleteEntity;
import lombok.Getter;

import java.util.Arrays;
import java.util.Objects;

/**
 * @author ll
 */
@Getter
public enum FanIndex implements EnumTrans<CompleteEntity.FanIndex, FanIndex> {
    /**
     * 风扇1
     */
    FAN_INDEX_1(CompleteEntity.FanIndex.FAN_INDEX_1, 0),
    /**
     * 风扇2
     */
    FAN_INDEX_2(CompleteEntity.FanIndex.FAN_INDEX_2, 1),
    /**
     * 风扇3
     */
    FAN_INDEX_3(CompleteEntity.FanIndex.FAN_INDEX_3, 2),
    /**
     * 风扇4
     */
    FAN_INDEX_4(CompleteEntity.FanIndex.FAN_INDEX_4, 3),
    ;

    private final Integer id;
    private final String name;
    private final CompleteEntity.FanIndex fanIndex;

    /**
     * 风扇序号
     */
    private final Integer index;

    FanIndex(CompleteEntity.FanIndex fanIndex, Integer index) {
        this.id = fanIndex.getNumber();
        this.name = fanIndex.name();
        this.fanIndex = fanIndex;
        this.index = index;
    }

    /**
     * @param fanIndex fanIndex
     * @return 对应的类型
     */
    public static FanIndex getScheduleTypeStruct(CompleteEntity.FanIndex fanIndex) {
        Objects.requireNonNull(fanIndex, "fanIndex can not be null.");
        return Arrays.stream(FanIndex.values())
                .filter(e -> e.name.equals(fanIndex.name()))
                .findFirst()
                .orElseThrow(() -> new ParameterException(ExceptionEnum.NOT_EXIST_ENUM));
    }

    @Override
    public CompleteEntity.FanIndex toProto() {
        return this.fanIndex;
    }

    @Override
    public FanIndex formProto(CompleteEntity.FanIndex fanIndex) {
        return getScheduleTypeStruct(fanIndex);
    }
}

